/**
 * 
 */
package com.example.speedwaymanager;

import android.graphics.Color;
import android.os.Bundle;
import android.provider.CalendarContract.Colors;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * @author lnowak
 *
 */
public class HeatFragment extends Fragment {
	
	public static final String ARG_OBJECT = "object";
	private CGameManager gm = CGameManager.getInstance();
	private View rootView;
	private TextView tv;
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        // Inflate the layout for this fragment             
        rootView = inflater.inflate(R.layout.heat_fragment, container, false);
        Bundle args = getArguments();
       // ((TextView) rootView.findViewById(R.id.text_fragmentHeat)).setText(Integer.toString(args.getInt(ARG_OBJECT)));
        initView(args.getInt(ARG_OBJECT));
        return rootView;
    }
	
	/**
	 * initialize heat's view
	 * @param i Heat number
	 */
	private void initView(int i)
	{
		if ( i < gm.get_match().get_heats().size() )
		{
			CHeat heat = gm.get_match().get_heats().get(i);
			CHeat schedule = gm.get_match().get_schedule().get_heat().get(i);
			
			// numbers
			tv = (TextView) rootView.findViewById(R.id.TextView_hNo1);
			CGate gate0 = gm.get_match().get_schedule().get_heat().get(i).get_gates().get(0);				
			tv.setText(((Integer)gate0._no).toString());
			tv.setTextColor(gate0.translateColor(gate0._color));
//			Log.d(getTag(), "init view heat number " + i);
			//tv.setTextColor(returnAndroidColor(getColor(schedule, heat.get_riders().get(0).get_number())));
			
			tv = (TextView) rootView.findViewById(R.id.TextView_hNo2);
			CGate gate1 = gm.get_match().get_schedule().get_heat().get(i).get_gates().get(1);				
			tv.setText(((Integer)gate1._no).toString());
			tv.setTextColor(gate1.translateColor(gate1._color));
			//tv.setTextColor(returnAndroidColor(getColor(schedule, heat.get_riders().get(1).get_number())));
			
			tv = (TextView) rootView.findViewById(R.id.TextView_hNo3);
			CGate gate2 = gm.get_match().get_schedule().get_heat().get(i).get_gates().get(2);				
			tv.setText(((Integer)gate2._no).toString());
			tv.setTextColor(gate2.translateColor(gate2._color));
			//tv.setTextColor(returnAndroidColor(getColor(schedule, heat.get_riders().get(2).get_number())));
			
			tv = (TextView) rootView.findViewById(R.id.TextView_hNo4);
			CGate gate3 = gm.get_match().get_schedule().get_heat().get(i).get_gates().get(3);				
			tv.setText(((Integer)gate3._no).toString());
			tv.setTextColor(gate3.translateColor(gate3._color));
			//tv.setTextColor(returnAndroidColor(getColor(schedule, heat.get_riders().get(3).get_number())));
			
			// riders
			tv = (TextView) rootView.findViewById(R.id.TextView_hRider1);
			tv.setText(gm.get_match().get_rider().get(gate0._no).toString());
			tv.setTextColor(gate0.translateColor(gate0._color));
			
			tv = (TextView) rootView.findViewById(R.id.TextView_hRider2);
			tv.setText(gm.get_match().get_rider().get(gate1._no).toString());
			tv.setTextColor(gate1.translateColor(gate1._color));
			
			tv = (TextView) rootView.findViewById(R.id.TextView_hRider3);
			tv.setText(gm.get_match().get_rider().get(gate2._no).toString());
			tv.setTextColor(gate2.translateColor(gate2._color));
			
			tv = (TextView) rootView.findViewById(R.id.TextView_hRider4);
			tv.setText(gm.get_match().get_rider().get(gate3._no).toString());
			tv.setTextColor(gate3.translateColor(gate3._color));
			
			// points
			tv = (TextView) rootView.findViewById(R.id.TextView_hPts1);
			if ( heat.get_RaceCompleted() )
				tv.setText(gm.get_match().get_rider().get(gate0._no).get_heatResult()._heatPoints.toString());
			else
				tv.setText("0");
			tv.setTextColor(gate0.translateColor(gate0._color));
			
			tv = (TextView) rootView.findViewById(R.id.TextView_hPts2);
			if ( heat.get_RaceCompleted() )
				tv.setText(gm.get_match().get_rider().get(gate1._no).get_heatResult()._heatPoints.toString());
			else
				tv.setText("0");
			tv.setTextColor(gate1.translateColor(gate1._color));
			
			tv = (TextView) rootView.findViewById(R.id.TextView_hPts3);
			if ( heat.get_RaceCompleted() )
				tv.setText(gm.get_match().get_rider().get(gate2._no).get_heatResult()._heatPoints.toString());
			else
				tv.setText("0");
			tv.setTextColor(gate2.translateColor(gate2._color));
			
			tv = (TextView) rootView.findViewById(R.id.TextView_hPts4);
			if ( heat.get_RaceCompleted() )
				tv.setText(gm.get_match().get_rider().get(gate3._no).get_heatResult()._heatPoints.toString());
			else
				tv.setText("0");
			tv.setTextColor(gate3.translateColor(gate3._color));
		}
				
	}
	private EColors getColor(CHeat heat, int number)
	{
		for (CGate gate: heat.get_gates())
		{
			if ( gate._no == number)
				return gate._color;
		}
		return null;
	}
	
	private int returnAndroidColor(EColors color)
	{
		int ret = Color.BLACK;
		switch (color){
			case eRed:
				ret = Color.RED;
				break;
			case eBlue:
				ret = Color.BLUE;
				break;
			case eYellow:
				ret = Color.YELLOW;
				break;
			case eWhite:
				ret = Color.BLACK;
				break;
		}
		return ret;
	}
}
